<template>
  <div>
    <el-form :model="goods" :rules="rules" ref="goodsform" label-position="right" label-width="100px" class="goods-form">
      <div class="goods-title">基本信息</div>
      <!-- 基本信息 -->
      <div>
        <el-form-item label="是否自动发货" class="goods-form-item">
          <el-switch v-model="goods.service_goods_is_ship"  active-color="#13ce66" active-value="1" inactive-value="0"></el-switch>
        </el-form-item>
        <el-form-item label="商品标题" class="goods-form-item" prop="name">
          <el-input v-model="goods.name" class="goods-form-item-input" placeholder="请输入" size="small"></el-input>
          <div class="goods-form-item-text">限30个字，60个字符内</div> 
        </el-form-item>
        <el-form-item label="短标题" class="goods-form-item" prop="short_name">
          <el-input v-model="goods.short_name" class="goods-form-item-input" placeholder="请输入" size="small"></el-input>
          <div class="goods-form-item-text">限12个字</div> 
        </el-form-item>
        <el-form-item label="简单说明" class="goods-form-item" prop="simple_info">
          <el-input type="textarea" :rows="1" v-model="goods.simple_info" class="goods-form-item-input" placeholder="请输入" size="small"></el-input>
          <div class="goods-form-item-text">在商品详情页标题下面展示卖点信息，200字以内</div> 
        </el-form-item>
        <el-form-item label="商品货号" class="goods-form-item">
          <el-input v-model="goods.code" class="goods-form-item-input" placeholder="请输入" size="small"></el-input>
        </el-form-item>
        <el-form-item label="商品单位" class="goods-form-item" prop="unit">
          <el-input v-model="goods.unit" class="goods-form-item-input" placeholder="请输入" size="small"></el-input>
          <div class="goods-form-item-text">如：件、份、箱</div> 
        </el-form-item>
        <el-form-item :label="index === 0 ? '商品属性' : ''" class="goods-form-item" v-for="(item,index) in goods.attribute" :key="index">
          <el-input v-model="goods.attribute[index]" class="goods-form-item-input" placeholder="请输入" size="small"></el-input>
          <el-button type="primary" plain class="goods-form-item-button" v-if="index === 0" @click="goods.attribute.push('')" size="small">添加</el-button>
          <el-button type="danger"  plain class="goods-form-item-button" v-else @click="goods.attribute.splice(index,1)" size="small">删除</el-button>
          <div class="goods-form-item-text" v-if="index === 0">如：品牌、保质期等。在商品详情顶部，以表格的形式显示，最多6条</div> 
        </el-form-item>
        <el-form-item label="分类" class="goods-form-item">
          <div style="width: 50%;">
            <el-select v-model="firstGroupId" placeholder="选择一级分类" style="width: calc(50% - 8px)" size="small" @change="selectGoodsGroup">
              <el-option :label="item.name" :value="item.id" v-for="item in goodGroupList" :key="item.id"></el-option>             
            </el-select>
            <el-select v-model="goods.m_category_id" placeholder="选择二级分类" style="width: calc(50% - 8px);margin-left:10px" size="small"
              v-if="firstGroup.length>0">
              <el-option :label="item.name" :value="item.id" v-for="item in firstGroup" :key="item.id"></el-option>             
            </el-select>
          </div>
        </el-form-item>
        <el-form-item label="销售区域" class="goods-form-item">
          <el-select v-model="goods.city_group_id" placeholder="选择销售区域" class="goods-form-item-input" size="small">
            <el-option label="全国" value=""></el-option>
            <el-option :label="item.name" :value="item.id" v-for="item in goodsAreaList" :key="item.id"></el-option>
          </el-select>
          <div class="goods-form-item-text">选择后代表这个商品只能在这个区域显示销售，不选择则是全国销售</div> 
        </el-form-item>
        <el-form-item label="增值服务" class="goods-form-item">
          <div style="width:50%;display:inline-flex">
            <el-input v-model="labels[index]" :style="{flex: 1, 'margin-left': index === 0 ? '0' : '15px'}" placeholder="请输入" size="small" v-for="(item, index) in labels" :key="index"></el-input>
          </div>          
          <div class="goods-form-item-text">显示在店铺信息下方，最多4个，每个最多4个字</div> 
        </el-form-item>
        <el-form-item label="商品主图" class="goods-form-item" prop="pic_urls">
          <el-button type="primary" plain @click="openMainPicPanel" size="small">选择图片</el-button>
          <div class="goods-form-item-text" style="width:calc(100% - 150px)">建议尺寸：750×750，最多上传5张，支持png、jpg、jpeg格式，大小不超过500KB</div> 
        </el-form-item>
        <el-form-item class="goods-form-item" v-if="mainPicList.length>0">
          <l-pic v-for="(item,index) in mainPicList" :key="index"
            :picurl="item"
            :pickey="index"
            :picdata="item"
            :picmargin="'-20px 10px 10px 10px'"
            :size="{ width:80, height:80 }"
            :piclist="mainPicList"
            @delPic="delMainPic"/>
        </el-form-item>
        <el-form-item label="主图缩略图" class="goods-form-item">
          <el-button type="primary" plain @click="openBriefPicPanel" size="small">选择图片</el-button>
          <div class="goods-form-item-text" style="width:calc(100% - 150px)">缩略图用于显示在首页，选择大图模式，建议上传600×350，最多上传一张，支持png、jpg、jpeg格式，大小不超过500KB</div> 
        </el-form-item>
        <el-form-item class="goods-form-item" v-if="briefPicList.length>0">
          <l-pic v-for="(item,index) in briefPicList" :key="index"
            :picurl="item"
            :pickey="index"
            :picdata="item"
            :picmargin="'-20px 10px 10px 10px'"
            :size="{ width:80, height:80 }"
            :piclist="briefPicList"
            @delPic="delBriefMainPic"/>
        </el-form-item>
        <el-form-item label="主视频" class="goods-form-item">
          <div style="width:50%;display:inline-block">
             
            <span style="padding:0 10px 0 20px">视频链接</span>  
            <el-input v-model="goods.video_url" class="goods-form-item-input" placeholder="请输入" size="small" style="width:calc(100% - 180px)"></el-input>        
          </div>
          <div class="goods-form-item-text">缩略图用于显示在首页，选择大图模式，建议上传600×350，最多上传一张，支持png、jpg、jpeg格式，大小不超过500KB</div> 
        </el-form-item>
        <el-form-item class="goods-form-item" v-if="goods.video_url !== ''" style="height:150px">
          <video :src="goods.video_url" controls="controls" class="goods-form-item-video">
            您的浏览器不支持 video 标签。
          </video>
        </el-form-item>
      </div>

      <div class="goods-title">商品详情</div>
      <!-- 商品详情 -->
      <div style="padding: 10px 20px">
        <tinymce v-model="goods.detail_info" :height="300" width="100%" /> 
      </div>

      <div class="goods-title">拼团设置</div>
      <!-- 拼团设置 -->
      <div>
        <el-form-item label="开启拼团" class="goods-form-item">
          <el-switch v-model="goods.is_open_assemble"  active-color="#13ce66" active-value="1" inactive-value="0"></el-switch>
          <div class="goods-form-item-button-text">开启后，该商品进入拼团模式，类似拼多多最低2人以上才能成交。注：同一商品不能同时开启秒杀、拼团、砍价，只能开启其中一种营销模式</div> 
        </el-form-item>
        <transition name="el-zoom-in-top">
          <div v-if="goods.is_open_assemble === '1'">
            <el-form-item label="单独购买" class="goods-form-item">
              <el-switch v-model="goods.is_self"  active-color="#13ce66" active-value="1" inactive-value="0"></el-switch>
              <div class="goods-form-item-button-text">是否允许该商品以原价单独购买</div> 
            </el-form-item>
            <el-form-item label="虚拟成团" class="goods-form-item">
              <el-switch v-model="goods.is_automatic"  active-color="#13ce66" active-value="1" inactive-value="0"></el-switch>
              <div class="goods-form-item-button-text">开启后，未成团的团会在最后五分钟虚拟成团，以便成交订单</div> 
            </el-form-item>
            <el-form-item label="老带新" class="goods-form-item">
              <el-switch v-model="goods.older_with_newer"  active-color="#13ce66" active-value="1" inactive-value="0"></el-switch>
              <div class="goods-form-item-button-text">开启后，该拼团仅能老客户分享给新客户下单</div> 
            </el-form-item>
            <el-form-item label="开团人优惠" class="goods-form-item">
              <el-switch v-model="goods.is_leader_discount"  active-color="#13ce66" active-value="1" inactive-value="0"></el-switch>
              <div class="goods-form-item-button-text">开启后，可以设置给开团的团长优惠多少比例</div> 
            </el-form-item>
            <el-form-item label="显示团长优惠" class="goods-form-item">
              <el-switch v-model="goods.is_show"  active-color="#13ce66" active-value="1" inactive-value="0"></el-switch>
              <div class="goods-form-item-button-text">开启后，将把团长的优惠显示在前端</div> 
            </el-form-item>
            <el-form-item label="拼团类型" class="goods-form-item">
              <el-radio-group v-model="goods.tuan_type" size="small" @change="tuanTypeChange">
                <el-radio label="1" border>多人团</el-radio>
                <el-radio label="2" border>阶级团</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item class="goods-form-item" v-for="(item, index) in groupTypeList" :key="index">
              <div class="group-type">
                <div>
                  <div>
                    <span class="group-type-title">成团人数</span>
                    <el-input v-model="item.assemble_number" class="group-type-input" placeholder="请输入" size="small"></el-input>
                  </div>
                  <div v-if="index === 0" class="group-type-text">该团最终成团人数，最低2人</div>
                </div>
                <div>
                  <div>
                    <span class="group-type-title" style="width:100px">开团优惠比例</span>
                    <el-input v-model="item.assemble_group_discount" class="group-type-input" style="width:calc(100% - 145px)" placeholder="请输入" size="small"></el-input>
                    <span style="padding-left:5px;color:#90939">%</span>
                  </div>
                  <div v-if="index === 0" class="group-type-text">给开团人的优惠比例，如填10，即10元减1元</div>
                </div>
                <div>
                  <div>
                    <span class="group-type-title" style="width:110px">阶梯团优惠比例</span>
                    <el-input v-model="item.discount" class="group-type-input" style="width:calc(100% - 155px)" placeholder="请输入" size="small"></el-input>
                    <span style="padding-left:5px;color:#90939">%</span>
                  </div>
                  <div v-if="index === 0" class="group-type-text">以规格里的拼团价为基准，每个阶梯团优惠多少钱</div>
                </div>
              </div>
              <div v-if="goods.tuan_type === '2'" style="display:inline-block">
                <el-button type="primary" plain class="goods-form-item-button" v-if="index === 0" 
                  @click="groupTypeList.push({ assemble_number: '', assemble_group_discount: '', discount: '' })" size="small">添加</el-button>
                <el-button type="danger"  plain class="goods-form-item-button" v-else 
                  @click="groupTypeList.splice(index,1)" size="small">删除</el-button>
              </div>
            </el-form-item>
          </div>
        </transition>
      </div>

      <div class="goods-title" style="margin-top:50px">砍价设置</div>
      <!-- 砍价设置 -->
      <div>
        <el-form-item label="开启砍价" class="goods-form-item">
          <el-switch v-model="cutPrice.isOpen"  active-color="#13ce66" active-value="1" inactive-value="0"></el-switch>
          <div class="goods-form-item-button-text">开启后，该商品进入砍价模式，参与人越多，优惠越多，以最终砍到的价格成交。注：同一商品不能同时开启秒杀、拼团、砍价，只能开启其中一种营销模式</div> 
        </el-form-item>
        <transition name="el-zoom-in-top">
          <div v-if="cutPrice.isOpen === '1'">
            <el-form-item label="活动时间" class="goods-form-item">
              <el-date-picker v-model="cutPrice.activeTime" class="goods-form-item-input" type="datetimerange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" size="small"></el-date-picker>
            </el-form-item>
            <el-form-item label="单独购买" class="goods-form-item">
              <el-switch v-model="cutPrice.isSelf"  active-color="#13ce66" active-value="1" inactive-value="0"></el-switch>
              <div class="goods-form-item-text">开启后，该商品支持原价格直接购买</div> 
            </el-form-item>
            <el-form-item label="虚拟砍价数" class="goods-form-item">
              <el-input v-model="cutPrice.virtualCount" class="goods-form-item-input" placeholder="请输入" size="small" style="width: 20%"></el-input>
            </el-form-item>
            <el-form-item label="虚拟帮砍人数" class="goods-form-item">
              <el-input v-model="cutPrice.virtualPeopCount" class="goods-form-item-input" placeholder="请输入" size="small" style="width: 20%"></el-input>
            </el-form-item>
            <el-form-item label="最终底价" class="goods-form-item">
              <el-input v-model="cutPrice.floorPrice" class="goods-form-item-input" placeholder="请输入" size="small" style="width: 20%"></el-input>
              <div class="goods-form-item-text" style="width:calc(80% - 100px)">开启后，该商品支持原价格直接购买</div> 
            </el-form-item>
            <el-form-item label="好友帮砍次数" class="goods-form-item">
              <el-input v-model="cutPrice.friendCutCount" class="goods-form-item-input" placeholder="请输入" size="small" style="width: 20%"></el-input>
              <div class="goods-form-item-text" style="width:calc(80% - 100px)">同一个人在该商品发起的砍价最多能砍多少次。注：一个发起砍价活动里，一个人在砍一次</div> 
            </el-form-item>
            <el-form-item label="砍价时间限制" class="goods-form-item">
              <el-input v-model="cutPrice.cutTime" class="goods-form-item-input" placeholder="请输入" size="small" style="width: 20%"></el-input>
              <div class="goods-form-item-text" style="width:calc(80% - 100px)">默认24小时，超过这个时间，该发起的砍价将失败</div> 
            </el-form-item>
            <el-form-item label="砍价规则" class="goods-form-item">
              <el-button type="primary" plain size="small" @click="cutPrice.cutRules.push({price: '', start: '', end: ''})">添 加</el-button>
              <div class="goods-form-item-button-text" >例如设置了大于100元，每次砍价5—10元，则在此区间每次都会在5-10之间随机抽取一个数字砍掉减去。价格砍到哪个区间，就触发哪个规则</div> 
            </el-form-item>
            <el-form-item class="goods-form-item" v-for="(item, index) in cutPrice.cutRules" :key="index">
               <el-row :gutter="20">
                <el-col :span="6" class="cutprice-col">
                  <span class="cutprice-title">金额大于</span>
                  <el-input v-model="item.price" placeholder="请输入" size="small" style="width:calc(100% - 90px);margin-right:15px"></el-input>
                </el-col>
                <el-col :span="8">
                  <span class="cutprice-title">活动时间</span>
                  <el-input v-model="item.start" placeholder="请输入" size="small" style="width:calc(50% - 60px)"></el-input>
                  <span style="padding:0 7px;color: #909399;">至</span>
                  <el-input v-model="item.end" placeholder="请输入" size="small" style="width:calc(50% - 60px)"></el-input>
                </el-col>
                <el-col :span="2">
                  <el-button type="danger"  plain class="goods-form-item-button" v-if="index > 0"
                    @click="cutPrice.cutRules.splice(index,1)" size="small">删除</el-button>
                </el-col>
                <el-col :span="8"></el-col>
              </el-row>
            </el-form-item>
          </div>
        </transition>
      </div>

      <div class="goods-title">价格规格库存</div>
      <!-- 价格规格库存 -->
      <div>
        <el-form-item label="规格设置" class="goods-form-item">
          <el-radio-group v-model="goods.stock_type" size="small" @change="goodsPropChange">
            <el-radio label="1" border>单规格</el-radio>
            <el-radio label="2" border>双规格</el-radio>
          </el-radio-group>
        </el-form-item>
        <!-- 规格设置 -->
        <div v-for="(item, index) in property" :key="index">
          <el-form-item label="规格名称" class="goods-form-item">
            <el-input v-model="item.name" class="goods-form-item-input" placeholder="请输入" size="small" style="width: 20%"></el-input>
          </el-form-item>
          <el-form-item label="规格值" class="goods-form-item">
            <el-tag v-for="(value, count) in item.value" :key="count" closable :disable-transitions="false" style="margin-right:10px"
              @close="property[index].value.splice(count,1)">{{ value.name }}</el-tag>
            <el-input 
              class="input-new-tag"
              v-if="isPropInput&&propEditIndex === index"
              v-model="propValue"
              ref="savePropInput"
              size="small"
              @keyup.enter.native="setPropValue(index)"
              @blur="setPropValue(index)"
            >
            </el-input>
            <el-button v-else class="button-new-tag" size="small" @click="showPropInput(index)">+ 规格值</el-button>
          </el-form-item>
        </div>
        <!-- 规格明细表格 -->
        <el-divider content-position="left">规格明细</el-divider>
        <div style="padding: 10px 0" v-for="(item, index) in property" :key="index+200">
          <div class="prop-table">
            <el-row class="prop-table-header">
              <el-col :span="2">图片</el-col>
              <el-col :span="2">名称</el-col>
              <el-col :span="2">规格</el-col>
              <el-col :span="3">现价(元)</el-col>
              <el-col :span="3">划线价(元)</el-col>
              <el-col :span="3">库存</el-col>
              <el-col :span="3">货号</el-col>
              <el-col :span="3">成本价</el-col>
              <el-col :span="3">拼团价</el-col>
            </el-row>
            <div v-if="item.value.length === 0" style="padding: 15px 0">暂无规格值数据</div>
            <el-row class="prop-table-row" v-for="(value, key) in item.value" :key="key" v-else>
              <el-col :span="2" class="prop-table-row-col">
                <div class="img-box-button" v-if="value.img === ''" @click="disPropPicPanel = true; propImgIndex = index; propImgKey = key">
                  <i class="el-icon-plus" style="position:relative;bottom:4px;font-size:24px;font-weight:600;color:#999"></i>
                </div>
                <l-pic :picurl="value.img" :size="{ width: 50, height: 50 }" picmargin="5px auto" v-else @delPic="value.img = ''"/>
              </el-col>
              <el-col :span="2" class="prop-table-row-col">{{ item.name }}</el-col>
              <el-col :span="2" class="prop-table-row-col">{{ value.name }}</el-col>
              <el-col :span="3" class="prop-table-row-col">
                <el-input v-model="value.price" size="small" style="width:80%"></el-input>
              </el-col>
              <el-col :span="3" class="prop-table-row-col">
                <el-input v-model="value.floorPrice" size="small" style="width:80%"></el-input>
              </el-col>
              <el-col :span="3" class="prop-table-row-col">
                <el-input v-model="value.number" size="small" style="width:80%"></el-input>
              </el-col>
              <el-col :span="3" class="prop-table-row-col">
                <el-input v-model="value.code" size="small" style="width:80%"></el-input>
              </el-col>
              <el-col :span="3" class="prop-table-row-col">
                <el-input v-model="value.cost_price" size="small" style="width:80%"></el-input>
              </el-col>
              <el-col :span="3" class="prop-table-row-col">
                <el-input v-model="value.assemble_price" size="small" style="width:80%"></el-input>
              </el-col>
            </el-row>
          </div>        
        </div>

        <el-form-item label="商品价格" class="goods-form-item" prop="price">
          <el-input v-model="goods.price" class="goods-form-item-input" placeholder="请输入" size="small" style="width: 20%"></el-input>
          <div class="goods-form-item-text" style="width:calc(80% - 100px)">商品销售的价格</div> 
        </el-form-item>
        <el-form-item label="划线价" class="goods-form-item">
          <el-input v-model="goods.line_price" class="goods-form-item-input" placeholder="请输入" size="small" style="width: 20%"></el-input>
        </el-form-item>
        <el-form-item label="库存" class="goods-form-item" prop="stocks">
          <el-input v-model="goods.stocks" class="goods-form-item-input" placeholder="请输入" size="small" style="width: 20%"></el-input>
          <div class="goods-form-item-text" style="width:calc(80% - 100px)">库存为0时，会放到[已售罄]的商品列表里，保存后，买家看到的商品可售库存同步更新</div> 
        </el-form-item>
        <el-form-item class="goods-form-item">
          <el-switch v-model="cutPrice.isOpen"  active-color="#13ce66" active-value="1" inactive-value="0"></el-switch>
          <div class="goods-form-item-text" style="width:calc(80% - 100px)">库存为0时，是否自动下架，如不开启，库存为0，商品还会在首页正常显示，显示已售完</div> 
        </el-form-item>
        <el-form-item label="虚拟销量" class="goods-form-item">
          <el-input v-model="goods.sales_number" class="goods-form-item-input" placeholder="请输入" size="small" style="width: 20%"></el-input>
          <div class="goods-form-item-text" style="width:calc(80% - 100px)">前端显示的虚拟销量，增加商品热度</div> 
        </el-form-item>
        <el-form-item label="限量" class="goods-form-item">
          <el-switch v-model="goods.is_limit"  active-color="#13ce66" active-value="1" inactive-value="0"></el-switch>
          <el-input v-model="goods.limit_number" class="goods-form-item-input" placeholder="请输入" size="small" style="width: 20%"></el-input>
          <div class="goods-form-item-text" style="width:calc(80% - 100px)">限制单个用户最多购买数</div> 
        </el-form-item>
        <el-form-item label="绑定自提点" class="goods-form-item">
          <el-select value="1" style="width:20%" size="small">
            <el-option label="未选择" value="1"></el-option>             
          </el-select>
          <div class="goods-form-item-text" style="width:calc(80% - 100px)">绑定自提点后，该商品只能由该自提点的微信核销，适合服务商品，或者特殊指定自提点的商品</div> 
        </el-form-item>
      </div>

      <div class="goods-title">佣金设置</div>
      <!-- 佣金设置 -->
      <div>
        <el-form-item label="开启单独设置" class="goods-form-item">
          <el-switch v-model="other"  active-color="#13ce66" active-value="1" inactive-value="0"></el-switch>
          <div class="goods-form-item-text" style="width:calc(80% - 100px)">开启后，佣金设置将按照此处设置的比例计算，如不开启，将走总设置</div> 
        </el-form-item>
        <el-form-item label="团长佣金" class="goods-form-item">
          <el-input v-model="goods.commission_selfleader_ratio" class="goods-form-item-input" placeholder="请输入" size="small" style="width: 20%"></el-input>
          <div class="goods-form-item-text" style="width:calc(80% - 100px)">%</div>
        </el-form-item>
        <el-form-item label="自提点佣金" class="goods-form-item">
          <el-input v-model="goods.commission_leader_ratio" class="goods-form-item-input" placeholder="请输入" size="small" style="width: 20%"></el-input>
          <div class="goods-form-item-text" style="width:calc(80% - 100px)">%</div>
        </el-form-item>
      </div>

      <div class="goods-title">其他信息</div>
      <!-- 其他信息 -->
      <div>
        <el-form-item label="上架时间" class="goods-form-item">
          <el-radio v-model="goods.start_type" label="0">立即上架</el-radio>
        </el-form-item>
        <el-form-item class="goods-form-item">
          <el-radio v-model="goods.start_type" label="1">自定义上架</el-radio>
          <el-date-picker v-model="goods.start_time" type="datetime" placeholder="选择上架时间" size="small"></el-date-picker>
        </el-form-item>
        <el-form-item class="goods-form-item">
          <el-radio v-model="goods.start_type" label="2">放入仓库，暂不售卖</el-radio>
        </el-form-item>
        <el-form-item label="下架时间" class="goods-form-item">
          <el-date-picker v-model="goods.end_time" type="datetime" placeholder="选择下架时间" size="small"></el-date-picker>
        </el-form-item>
        <el-form-item label="提货时间" class="goods-form-item">
          <el-date-picker v-model="goods.take_goods_time" type="datetime" placeholder="选择提货时间" size="small"></el-date-picker>
        </el-form-item>
        <el-form-item label="团长专属" class="goods-form-item">
          <el-switch v-model="goods.regimental_only"  active-color="#13ce66" active-value="1" inactive-value="0"></el-switch>
          <div class="goods-form-item-text" style="width:calc(80% - 100px)">开启后，该商品只有团长才能购买，未启用团购模式，无需开启</div> 
        </el-form-item>
        <el-form-item label="排序" class="goods-form-item">
          <el-input v-model="goods.sort" class="goods-form-item-input" placeholder="请输入" size="small" style="width: 20%"></el-input>
        </el-form-item>
      </div>
    </el-form>

    <div style="text-align: center;margin-top:50px">
      <el-button type="primary" style="padding:10px 30px;font-weight:600" @click="submit">提 交</el-button>
    </div>

    <!-- 相册面板 -->
    <Modal v-model="disPicPanel" width="1128" :footer-hide="true">
      <photo-album v-if="disPicPanel" :maxcount="maxPicCount" @selectPicData="getPicData"/>
    </Modal>

    <!-- 缩略图相册面板 -->
    <Modal v-model="disBriefPicPanel" width="1128" :footer-hide="true">
      <photo-album v-if="disBriefPicPanel" :maxcount="maxBriefPicCount" @selectPicData="getBriefPicData"/>
    </Modal>

    <!-- 规格图片选择面板 -->
    <Modal v-model="disPropPicPanel" width="1128" :footer-hide="true">
      <photo-album v-if="disPropPicPanel" :maxcount="1" @selectPicData="getPropPicData"/>
    </Modal>
  </div>
</template>

<script>
import { rules, cutPrice, property } from './goodsData' 
import { getGoodsGroupList, getGoodsArea, postGoodsVideoUrl, postGoods } from '@/api/addgoods'
import { putGoods } from '@/api/goods'
import PhotoAlbum from '@/components/PhotoAlbum'
import LPic from '@/components/lPic'
import Tinymce from '@/components/Tinymce'
import { getToken } from '@/utils/auth'

export default {
  name: 'Virtual',
  components: {
    PhotoAlbum,
    LPic,
    Tinymce
  },
  props: {
    goods: {
      type: Object,
      required: true
    },
    isAdd: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      // goods: { key: this.$store.state.app.activeApp.saa_key, service_goods_is_ship: '1', ...goods },
      rules: rules,

      goodGroupList: [], //商品分组
      firstGroup: [],//一级分类
      firstGroupId: '',//选择的一级分类的ID
      
      goodsAreaList: [],//商品销售区域
      
      labels: [ '', '', '', '' ],

      disPicPanel: false, //打开相册面板
      maxPicCount: 5,//最多选择图片张数

      mainPicList: [],//商品主图列表

      disBriefPicPanel: false,//打开缩略图面板
      maxBriefPicCount: 1,//缩略图最多选择张数
      briefPicList: [],//缩略图

      videoUrl: postGoodsVideoUrl(),//上传视频的url
      token: getToken(),

      groupTypeList:[{ assemble_number: '', assemble_group_discount: '', discount: '' }],

      cutPrice: cutPrice, //砍价数据
      property: property, //规格

      isPropInput: false,
      propEditIndex: 0,
      propValue: '',

      disPropPicPanel: false, //规格图片选择
      propImgIndex: 0,
      propImgKey: 0,

      other: '1'
    }
  },
  watch: {
    goodGroupList(newValue) { //商品分组
      if(newValue.length > 0) {
        this.firstGroupId = newValue[0].id;
        this.selectGoodsGroup();
      }
    },
    labels(newValue) { //增值服务
      this.goods.label = ''
      newValue.forEach((item,index) => {
        if(item.trim() !== ''){
          if(index < 3){
            this.goods.label += item + ','
          }else{
            this.goods.label += item
          }          
        }
      });
    },
    mainPicList(newValue) { //商品主图
      if(newValue.length === 0) {
        this.goods.pic_urls = '';
      }else{
        newValue.forEach((item,index) => {
          if(index < 4){
            this.goods.pic_urls += item + ',';
          }else{
            this.goods.pic_urls += item;
          }
        })
      }
    },
    groupTypeList(newValue) { //拼团类型
      if(this.goods.tuan_type === '1'){
        this.goods.assemble_number = newValue[0].assemble_number;
        this.goods.assemble_group_discount = newValue[0].assemble_group_discount;
      }
    }
  },
  mounted() {
    this.getGoodGroup();//获取商品分组
    this.getGoodsArea();//获取商品销售区域

    //设置labels
    this.labels = this.goods.label.split(',');
    for (let index = this.labels.length; index < 4; index++) {
      this.labels[index] = '';       
    }

    //设置商品主图
    this.mainPicList = [];
    this.goods.pic_urls.split(',').forEach(item => {
      if(item !== '' && item !== null && item !== undefined){
        this.mainPicList.push(item);
        this.maxPicCount --;
      }
    })

    //切换单多规格
    this.goodsPropChange()
  },
  methods: {
    /**
     * 提交
     */
    submit() {
      this.$refs.goodsform.validate(valid => {
        if(valid) {
          if (this.isAdd) {
            postGoods(this.goods).then(res => {
              if(res.status === 200) {
                this.$message.success('添加成功！');
                this.$router.push('/goods/list');
              }else{
                this.$message.error(res.message);
              }
            })
          }else{
            // const params = upDateFormat(this.goods);
            putGoods(this.goods).then(res => {
              if(res.status === 200) {
                this.$message.success('修改成功！');
                this.$router.push('/goods/list');
              }else{
                this.$message.error(res.message);
              }
            })
          }         
        }else {
          this.$message.error('请根据提示，完善商品信息！')
        }
      })
    },
    /**
     * 获取商品分组
     */
    getGoodGroup() {
      getGoodsGroupList({ key: this.$store.state.app.activeApp.saa_key }).then(res => {
        if(res.status === 200){
          this.goodGroupList = res.data;
        }else if(res.status === 204){
          this.goodGroupList = []
        }else{
          this.$message.error(res.message);
        }
      })
    },
    /**
     * 获取商品销售区域
     */
    getGoodsArea() {
      getGoodsArea({ key: this.$store.state.app.activeApp.saa_key, status: 1 }).then(res => {
        if(res.status === 200){
          this.goodsAreaList = res.data;
        }else if(res.status === 204){
          this.goodsAreaList = [];
        }else{
          this.$message.error(res.message);
        }
      })
    },
    /**
     * 选择商品分类
     */
    selectGoodsGroup() {
      const group = this.goodGroupList.find(item => item.id === this.firstGroupId);
      this.firstGroup = group === undefined ? [] : group.sub;
      if(this.firstGroup.length > 0){
        this.goods.m_category_id = this.firstGroup[0].id;
      }else{
        this.goods.m_category_id = this.firstGroupId;
      }
    },
    /**
     * 打开主图选择面板
     */
    openMainPicPanel() {
      if(this.maxPicCount > 0){
        this.disPicPanel = true;
      }else{
        this.$message.warning(`最多可选择5张图片，已选择${5-this.maxPicCount}张,不可选择`);
      }
    },
    /**
     * 获取选择到的主图数据
     */
    getPicData(data) {
      this.disPicPanel = false;
      data.forEach(item => {
        if(this.maxPicCount > 0) {
          this.mainPicList.push(item.pic_url);
          this.maxPicCount --;
        }else{
          this.$message.warning(`最多可选择5张图片，已选择${5-this.maxPicCount}张`);
          return false
        }
      })
    },
    /**
     * 删除主图
     */
    delMainPic(data) {
      
      this.mainPicList.splice(this.mainPicList.findIndex(item => item===data),1);
      this.maxPicCount++;
    },
    /**
     * 打开缩略图面板
     */
    openBriefPicPanel() {
      if(this.maxBriefPicCount > 0) {
        this.disBriefPicPanel = true;
      }else{
        this.$message.warning('最多可选择1张图片，已选择1张,不可选择');
      }
    },
    /**
     * 获取选择到的缩略图数据
     */
    getBriefPicData(data) {
      this.disBriefPicPanel = false;
      data.forEach(item => {
        if(this.maxBriefPicCount > 0) {
          this.briefPicList.push(item.pic_url);
          this.maxBriefPicCount --;
        }else{
          this.$message.warning('最多可选择1张图片，已选择1张');
          return false
        }
      })
    },
    /**
     * 删除缩略图
     */
    delBriefMainPic(data) {
      this.briefPicList.splice(this.briefPicList.findIndex(item => item===data),1);
      this.maxBriefPicCount++;
    },
    /**
     * 上传视频成功
     */
    videoSuccess(response) {
      if(response.status === 200) {
        this.$message.success('上传成功！');
        this.goods.video_id = response.data.video_id;
        this.goods.video_pic_url = response.data.video_pic_url;
        this.goods.video_url = response.data.video_url;
      }else{
        this.$message.error(response.message);
      }
    },
    /**
     * 切换拼团类型
     */
    tuanTypeChange() {
      if(this.goods.tuan_type === '1') {
        if(this.groupTypeList.length >= 2){
          this.groupTypeList.splice(1,this.groupTypeList.length-1)
        }      
      }
    },
    /**
     * 切换单多规格类型
     */
    goodsPropChange() {
      if(this.goods.stock_type === '1') {
        if(this.property.length > 1) {
          this.property.splice(1,this.property.length-1);
        }
      }else{
        if(this.property.length === 1) {
          this.property.push({name: '', value: []});
        }
      }
    },
    /**
     * 显示添加属性值输入框
     */
    showPropInput(index) {
      this.isPropInput = true;
      this.propEditIndex = index;
      this.$nextTick(() => {
        this.$refs.savePropInput[0].$refs.input.focus();
      })
    },
    /**
     * 新增规格值
     */
    setPropValue(index) {
      let value = this.propValue;
      if(value) {
        this.property[index].value.push({ name: value,price: '',floorPrice: '',cost_price: '',assemble_price: '',number: '', code: '', img: ''})
      }
      this.isPropInput = false;
      this.propValue = '';
    },
    /**
     * 获取选择的规格图片的值
     */
    getPropPicData(data) {
      this.disPropPicPanel = false;
      this.property[this.propImgIndex].value[this.propImgKey].img = data[0].pic_url;
    }
  },
}
</script>

<style lang="scss" scoped>
.goods-form{

}
.goods-title{
  height: 47px;
  width: 100%;
  line-height: 47px;
  font-size: 16px;
  font-weight: 600;
  letter-spacing: 2px;
  color: #8f8f8f;
  padding: 0 10px;
  background-color: #F4F8FB;
}
.goods-form-item{
  width: 100%;
  height: 50px;
  line-height: 50px;
  padding: 5px 0;
  box-sizing: border-box;
  &-input{
    width: 50%;
    height: 40px;
    line-height: 40px;
  }
  &-text{
    width: calc(50% - 100px);
    display: inline-block;
    height: 40px;
    line-height: 40px;
    padding: 0 0 0 20px;
    color: #c0c4cc;
    font-size: 14px;
    white-space:nowrap;
    overflow:hidden;
    text-overflow:ellipsis;
    vertical-align: middle;
  }
  &-button-text{
    width: calc(100% - 200px);
    display: inline-block;
    height: 40px;
    line-height: 40px;
    padding: 0 0 0 20px;
    color: #c0c4cc;
    font-size: 14px;
    white-space:nowrap;
    overflow:hidden;
    text-overflow:ellipsis;
    vertical-align: middle;
  }
  &-button{
    padding: 5px 10px;
    margin-left: 10px;
  }
  &-video{
    height: 150px;
    min-width: 200px;
    max-width: 100%;
  }
}

.group-type{
  width:calc(100% - 100px);
  display: inline-flex;
  &>div{
    flex: 1;
    padding-right: 10px;
  }
  &-title{
    // flex: 1;
    width: 80px;
    display: inline-block;
    padding: 0 5px 0 0;
    color: #909399;
    white-space:nowrap;
    overflow:hidden;
    text-overflow:ellipsis;
    vertical-align: middle;
    // text-align: right;
  }
  &-input{
    width: calc(100% - 120px);
    height: 40px;
    line-height: 40px;
  }
  &-text{
    width: 100%;
    color: #c0c4cc;
    font-size: 14px;
    white-space:nowrap;
    overflow:hidden;
    text-overflow:ellipsis;
  }
}
.cutprice-col{
  overflow:hidden;
  height: 40px;
}
.cutprice-title{
  width: 70px;
  display: inline-block;
  padding: 0 5px 0 0;
  color: #909399;
  white-space:nowrap;
  overflow:hidden;
  text-overflow:ellipsis;
  vertical-align: middle;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
.prop-table{
  border: 1px dashed #eee;
  text-align: center;
  &-header{
    height: 30px;
    line-height: 30px;
    background-color: #F4F8FB;
  }
  &-row{
    height: 60px;
    line-height: 60px;
    &-col{
      height: 60px;
    }
  }
}
.img-box-button{
  height: 50px;
  width: 50px;
  border: 2px dashed #eee;
  border-radius: 4px;
  margin: 5px auto;
  cursor: pointer;
}
</style>